class NodosController < ApplicationController
  # GET /nodos
  # GET /nodos.xml
  def index
    @nodos = Nodo.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @nodos }
    end
  end

  # GET /nodos/1
  # GET /nodos/1.xml
  def show
    @nodo = Nodo.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @nodo }
    end
  end

  # GET /nodos/new
  # GET /nodos/new.xml
  def new
    @nodo = Nodo.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @nodo }
    end
  end

  # GET /nodos/1/edit
  def edit
    @nodo = Nodo.find(params[:id])
  end

  # POST /nodos
  # POST /nodos.xml
  def create
    @nodo = Nodo.new(params[:nodo])

    respond_to do |format|
      if @nodo.save
        flash[:notice] = 'Nodo creado correctamente.'

        # si se graba el nodo genera las carreteras con los nodos colindantes.
        colindantes = @nodo.nodos_en_rango(10)
        colindantes.each do |c|
          params[:nodo1_id]= c
          params[:nodo2_id]=self.index
          params[:status]=0
          road= Road.new
          road.create
        end


        format.html { redirect_to(@nodo) }
        format.xml  { render :xml => @nodo, :status => :created, :location => @nodo }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @nodo.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /nodos/1
  # PUT /nodos/1.xml
  def update
    @nodo = Nodo.find(params[:id])

    respond_to do |format|
      if @nodo.update_attributes(params[:nodo])
        flash[:notice] = 'Nodo was successfully updated.'
        format.html { redirect_to(@nodo) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @nodo.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /nodos/1
  # DELETE /nodos/1.xml
  def destroy
    @nodo = Nodo.find(params[:id])
    @nodo.destroy

    respond_to do |format|
      format.html { redirect_to(nodos_url) }
      format.xml  { head :ok }
    end
  end
end
